//
//  Envelope.h
//  BigDogValueSynth
//
//  Created by Alistair Barker on 20/05/2013.
//  Copyright 2013 __MyCompanyName__. All rights reserved.
//  Version 1.1.0

#include "../JuceLibraryCode/JuceHeader.h"

class Envelope
{
public: 
    
    Envelope (double attack, double decay, double sustain, double release, double Fs);
    
    int getState() {return state;};    
    
    void setAttack(double value, double Fs) { attackInc = getIncValue(value, Fs); };
    void setDecay(double value, double Fs) { decayInc = getIncValue(value, Fs); };
    void setSustain(double value) { sustainLevel = value; };
    void setRelease(double value, double Fs) { releaseInc = getIncValue(value, Fs); };
    
    void setEnvelope(double attack, double decay, double sustain, double release, double Fs);

    
    void noteOn(float velocity);
    void noteOff();
    
    float getLinearGainAndTick();
    float getLogGainAndTick();
    
    enum  {
        kAttack,
        kDecay,
        kSustain,
        kRelease,
        kIdle
    };
    

private:
    
    double getCurve(double value);
    
    float tick();
    void processEnvGain ();
    double getIncValue(double value, double Fs);    
    
    float gain;

    double attackInc;
    double decayInc;
    double sustainLevel;
    double releaseInc;
    
    double position;
    int state;
    
    float gainAtNoteOff;
    float gainAtNoteOn;
    
    
    
};

